package com.wangyadong.hobby.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 10)
@Measurement(iterations = 10,time = 3)
public class JMHExample002 {

    @Benchmark
    public String stringAppend() {
        String s = "";
        for (int i = 0; i < 100; i++) {
            s += i;
        }
        return s;
    }

    @Benchmark
    public String stringBuilderAppend() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append(i);
        }
        return sb.toString();
    }

    @Benchmark
    public String stringBufferAppend() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 100; i++) {
            sb.append(i);
        }
        return sb.toString();
    }


    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder().include(JMHExample002.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(options).run();
    }
}
